package com.shishuo.cms.action.manage;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.UserWebEmail;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.UserWebEmailService;

@Controller
@RequestMapping("/admin/userWebEmail")
public class userWebEmailAction extends AdminBaseAction {

	@InitBinder("userWebEmail")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("userWebEmail.");
	}

	@Autowired
	private UserWebEmailService userWebEmailService;
	
	@AuthPassport
	@RequestMapping(value = "/sendEmail.htm", method = RequestMethod.GET)
	public String sendEmail(ModelMap modelMap, HttpServletRequest request) throws Exception
	{
		return "system/userWebEmail/sendEmail";
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/sendEmail.json", method = RequestMethod.POST)
	public JsonVo<String> addNewClasses(
			@ModelAttribute UserWebEmail userWebEmail,
			@RequestParam(value = "isSendToAllUser", defaultValue = "1") int isSendToAllUser,
			@RequestParam(value = "content", defaultValue = "") String content)
			throws Exception {
		userWebEmail.setContent(content);
		JsonVo<String> json = new JsonVo<String>();
		if (userWebEmail.getTitle() == null
				|| "".equals(userWebEmail.getTitle())) {
			json.getErrors().put("title", "标题不能为空");
		}
		if (userWebEmail.getContent() == null
				|| "".equals(userWebEmail.getContent())) {
			json.getErrors().put("content", "内容不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		userWebEmailService.sendEmail(userWebEmail,isSendToAllUser);
		json.setResult(true);
		json.setMsg("发送成功！");
		return json;
	}
	
}
